<html>
<head><meta charset="utf-8"><title>obtain generics of function in rustc_hir::Expr · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html">obtain generics of function in rustc_hir::Expr</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245758664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245758664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245758664">(Jul 12 2021 at 22:33)</a>:</h4>
<p>I have a <code>rustc_hir::Expr</code> and would like to obtain the generics of a function (I'm matching on <code>hir::ExprKind::Call</code>). How can I do this? I am able to obtain the path of the function, but there's no method to get the generics from that.</p>



<a name="245758777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245758777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245758777">(Jul 12 2021 at 22:34)</a>:</h4>
<p>^ ideally this would include inferred generics, but I'm pretty sure that's the case</p>



<a name="245759578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245759578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245759578">(Jul 12 2021 at 22:44)</a>:</h4>
<p>i think you need to resolve the path to a <code>DefId</code></p>



<a name="245759636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245759636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245759636">(Jul 12 2021 at 22:46)</a>:</h4>
<p>I have a <code>DefId</code> already, but don't see a way to get the generics from it.</p>



<a name="245759735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245759735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245759735">(Jul 12 2021 at 22:47)</a>:</h4>
<p>possibly <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.get_generics">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.get_generics</a> but that's going to be <code>hir::Generics</code>, which might not contain everything you need</p>



<a name="245760033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245760033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245760033">(Jul 12 2021 at 22:51)</a>:</h4>
<p>Not sure how I'd get the <code>Map</code> to begin with.</p>



<a name="245760188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245760188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245760188">(Jul 12 2021 at 22:53)</a>:</h4>
<p>Although I guess I can manually construct it</p>



<a name="245760207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245760207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245760207">(Jul 12 2021 at 22:53)</a>:</h4>
<p>maybe try the <code>generics_of</code> query</p>



<a name="245760483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245760483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245760483">(Jul 12 2021 at 22:57)</a>:</h4>
<p>the <code>Map</code> typically comes from <code>tcx.hir()</code>, and i'm not remembering off the top of my head how to get a <code>tcx</code> if you're not already in a part of the compiler where you have one around</p>



<a name="245760740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245760740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245760740">(Jul 12 2021 at 23:00)</a>:</h4>
<p>The <code>tcx.hir()</code> is what I was looking for I think. <code>generics_of</code> doesn't look like it gives me a way to get the concrete type, whereas the map lets me get a <code>GenericParam</code>.</p>



<a name="245761134"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245761134" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245761134">(Jul 12 2021 at 23:05)</a>:</h4>
<p><code>generics_of</code> gets you a <code>ty::Generics</code>; <code>get_generics</code> gets you a <code>hir::Generics</code>; they're useful for different things. like a <code>hir::Generics</code> will have the parsed trait bounds and <code>where</code> clauses</p>



<a name="245761262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245761262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245761262">(Jul 12 2021 at 23:06)</a>:</h4>
<p>Ultimately what I'm trying to do is check if a generic in <code>transmute</code> is a pointer, integer, or neither. This is in a method call, so <code>where</code> clauses aren't needed.</p>



<a name="245761318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245761318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245761318">(Jul 12 2021 at 23:07)</a>:</h4>
<p>oh, so you want the substituted generics?</p>



<a name="245761359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245761359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245761359">(Jul 12 2021 at 23:08)</a>:</h4>
<p>Whatever the final type resolves to. Whether it's inferred or not shouldn't matter — that is my end goal.</p>



<a name="245761403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245761403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245761403">(Jul 12 2021 at 23:08)</a>:</h4>
<p>Best to be clear to avoid an XY problem.</p>



<a name="245761443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245761443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245761443">(Jul 12 2021 at 23:08)</a>:</h4>
<p>So for <code>transmute::&lt;*const T, U&gt;(foo)</code>, I would like to get the <code>*const T</code> and <code>U</code> out, even if inferred.</p>



<a name="245762637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245762637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245762637">(Jul 12 2021 at 23:24)</a>:</h4>
<p>it sounds like you might be asking for concrete types, which you might not get until after type checking?</p>



<a name="245762949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245762949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245762949">(Jul 12 2021 at 23:28)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/213817-t-lang/topic/Stabilize.20const_fn_transmute.2C.20const_fn_union/near/245723465">said</a>:</p>
<blockquote>
<p>yes, it works on HIR which is fully typed</p>
</blockquote>
<p>I'm adding a lint in the <code>rustc_lints</code> crate if that makes a difference, which works on HIR. Ralf says that is typed, and that appears to be the case from my extremely limited purview into it.</p>



<a name="245763208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245763208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245763208">(Jul 12 2021 at 23:32)</a>:</h4>
<p>so in a function call to a generic function where the call site includes generic type arguments, that's going to be part of the <code>hir::Path</code>, which you probably already have? like there's a <code>hir::PathSegment::args()</code></p>



<a name="245763481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245763481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245763481">(Jul 12 2021 at 23:36)</a>:</h4>
<p>Would that include inferred generics?</p>



<a name="245763606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245763606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245763606">(Jul 12 2021 at 23:38)</a>:</h4>
<p>i think it wouldn't; HIR tends to be pretty close to the program text. some type information might get resolved, but only by mapping paths to <code>DefId</code>s, i think?</p>



<a name="245763632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245763632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245763632">(Jul 12 2021 at 23:39)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> ^^ if you have further knowledge? You also know exactly what I'm trying to do</p>



<a name="245763668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245763668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245763668">(Jul 12 2021 at 23:39)</a>:</h4>
<p>i've mostly dealt with the HIR when working on error reporting stuff. it looks like THIR might have fully resolved type information after type checking? i haven't worked with it yet though</p>



<a name="245763726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245763726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245763726">(Jul 12 2021 at 23:40)</a>:</h4>
<p>Nor have I, hence my confusion.</p>



<a name="245765197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245765197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245765197">(Jul 13 2021 at 00:06)</a>:</h4>
<p>oh, it looks like there's a <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/context/struct.LateContext.html#method.maybe_typeck_results">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/context/struct.LateContext.html#method.maybe_typeck_results</a> that might give you access to type checking results, which i assume would include resolved types at a function call site</p>



<a name="245765378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245765378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245765378">(Jul 13 2021 at 00:09)</a>:</h4>
<p>I'll check that out once I'm back on my laptop</p>



<a name="245779570"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245779570" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245779570">(Jul 13 2021 at 04:56)</a>:</h4>
<p>Not sure how I missed that given it's <em>literally</em> a parameter to the method I'm implementing. That's what I was looking for, though. Specifically the <code>.node_type()</code> method. Thank you!</p>



<a name="245779572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245779572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245779572">(Jul 13 2021 at 04:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="245610">Jacob Pratt</span> has marked this topic as resolved.</p>



<a name="245787456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245787456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245787456">(Jul 13 2021 at 07:27)</a>:</h4>
<p>Well...<em>that</em> will likely work, but getting the generic parameters is now my issue. I tried</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">generic_params</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cx</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">tcx</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">hir</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">get_generics</span><span class="p">(</span><span class="n">def_id</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">expect</span><span class="p">(</span><span class="s">"`transmute` has generic parameters"</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">params</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>to no avail, as the map returned only considers the literal syntax used.</p>



<a name="245787458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245787458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245787458">(Jul 13 2021 at 07:27)</a>:</h4>
<p><span class="user-mention silent" data-user-id="245610">Jacob Pratt</span> has marked this topic as unresolved.</p>



<a name="245787519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245787519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245787519">(Jul 13 2021 at 07:28)</a>:</h4>
<p>so now I just need assistance getting the <em>inferred</em> type during a lint in <code>rustc_lints</code></p>



<a name="245791461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245791461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245791461">(Jul 13 2021 at 08:15)</a>:</h4>
<p>Turns out this whole time the solution was sitting in the same file <span aria-label="face palm" class="emoji emoji-1f926" role="img" title="face palm">:face_palm:</span> ah well lol</p>



<a name="245792420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245792420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245792420">(Jul 13 2021 at 08:26)</a>:</h4>
<p><span class="user-mention" data-user-id="245610">@Jacob Pratt</span> this all sounds way too complicated</p>



<a name="245792450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245792450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245792450">(Jul 13 2021 at 08:26)</a>:</h4>
<p>there are various <code>ty</code> methods you can call to get a <code>Ty</code>, and that should be all</p>



<a name="245792454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245792454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245792454">(Jul 13 2021 at 08:26)</a>:</h4>
<p>Oh it was. I was looking through some other code and found another lint that already handles transmute</p>



<a name="245792471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245792471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245792471">(Jul 13 2021 at 08:26)</a>:</h4>
<p>if you interact explicitly with generics I think that's the wrong way</p>



<a name="245792477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245792477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245792477">(Jul 13 2021 at 08:26)</a>:</h4>
<p>It's the <code>get_transmute_from_to</code> method. Once I have that it's stupidly simple</p>



<a name="245792505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245792505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245792505">(Jul 13 2021 at 08:27)</a>:</h4>
<p>Needless to say I've never done this before</p>



<a name="245792618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245792618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245792618">(Jul 13 2021 at 08:28)</a>:</h4>
<p>ah I see -- yes <code>get_transmute_from_to</code> is also a way. I would have reached for the types of various terms (argument, whole call) but either way is fine. :)</p>



<a name="245792650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245792650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245792650">(Jul 13 2021 at 08:28)</a>:</h4>
<p><span class="user-mention silent" data-user-id="245610">Jacob Pratt</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr/near/245792505">said</a>:</p>
<blockquote>
<p>Needless to say I've never done this before</p>
</blockquote>
<p>yeah, I also rarely work on the HIR so each time I do I need to go find some other code and copy-paste what it does^^</p>



<a name="245792686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245792686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245792686">(Jul 13 2021 at 08:29)</a>:</h4>
<p>the rustc API docs are also extremely helpful, e.g. <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.Expr.html">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.Expr.html</a></p>



<a name="245792723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245792723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245792723">(Jul 13 2021 at 08:29)</a>:</h4>
<p>The problem is that presumes I know what I'm doing <span aria-label="stuck out tongue" class="emoji emoji-1f61b" role="img" title="stuck out tongue">:stuck_out_tongue:</span></p>



<a name="245792739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245792739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245792739">(Jul 13 2021 at 08:29)</a>:</h4>
<p>When you're clueless docs only help so much</p>



<a name="245793047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245793047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245793047">(Jul 13 2021 at 08:32)</a>:</h4>
<p>So the lint I've got implemented (successfully!) checks for <code>transmute</code> from either a pointer or reference to any int/uint/float. I should also be able to do this for <code>as</code> casts, which should be quite simple <span aria-label="fingers crossed" class="emoji emoji-1f91e" role="img" title="fingers crossed">:fingers_crossed:</span> Union accesses, that's someone else's problem more than likely</p>



<a name="245795314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245795314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245795314">(Jul 13 2021 at 08:55)</a>:</h4>
<p>One question regarding the lint, though. Are ptr→int transmutes/casts only UB in const contexts or are they always UB?</p>



<a name="245795783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245795783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245795783">(Jul 13 2021 at 09:00)</a>:</h4>
<p>It is not quite clear at the moment. They may always be UB or they may not be.</p>



<a name="245795859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245795859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245795859">(Jul 13 2021 at 09:00)</a>:</h4>
<p><a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/286">https://github.com/rust-lang/unsafe-code-guidelines/issues/286</a></p>



<a name="245795982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245795982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245795982">(Jul 13 2021 at 09:01)</a>:</h4>
<p>Yeah, I skimmed over the issue and could not come to a conclusion. I guess I'll leave the lint more stringent with a note that it's not exactly clear</p>



<a name="245857250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245857250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245857250">(Jul 13 2021 at 17:17)</a>:</h4>
<p>such <code>as</code> casts as forbidden</p>



<a name="245857311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245857311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245857311">(Jul 13 2021 at 17:18)</a>:</h4>
<p>so also linting them would be confusing I think^^</p>



<a name="245857328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245857328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245857328">(Jul 13 2021 at 17:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="245610">Jacob Pratt</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr/near/245795314">said</a>:</p>
<blockquote>
<p>One question regarding the lint, though. Are ptr→int transmutes/casts only UB in const contexts or are they always UB?</p>
</blockquote>
<p>only in consts, for now</p>



<a name="245857348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/obtain%20generics%20of%20function%20in%20rustc_hir%3A%3AExpr/near/245857348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/obtain.20generics.20of.20function.20in.20rustc_hir.3A.3AExpr.html#245857348">(Jul 13 2021 at 17:18)</a>:</h4>
<p>let's not stress out people about the non-const case until we are sure we have to^^</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>